package com.kehutong.house.util;

import com.kehutong.house.entity.House;

/**
 * A sorting comparator to sort strings numerically, ie [1, 2, 10], as opposed
 * to [1, 10, 2].
 */
public  class CompareUtil
{
    public static final String  COMMA = "," ;

    private static int compareRight(String a, String b) {
        int bias = 0;
        int ia = 0;
        int ib = 0;

        for (;; ia++, ib++) {
            char ca = charAt(a, ia);
            char cb = charAt(b, ib);

            if (!Character.isDigit(ca) && !Character.isDigit(cb)) {
                return bias;
            }
            else if (!Character.isDigit(ca)) {
                return -1;
            }
            else if (!Character.isDigit(cb)) {
                return +1;
            }
            else if (ca < cb) {
                if (bias == 0) {
                    bias = -1;
                }
            }
            else if (ca > cb) {
                if (bias == 0)
                    bias = +1;
            }
            else if (ca == 0 && cb == 0) {
                return bias;
            }
        }
    }


    public static String getNoHouseNameCompare(House h){
        StringBuffer str = new StringBuffer();
        String s = str.append(h.getCommunityName()).append(COMMA).append(h.getBuildName()).append(COMMA).append(h.getUnitName()).append(COMMA).append(h.getFloorName()).toString();
        return s ;
    }

    public static String getHasHouseCompare(House h){
        StringBuffer str = new StringBuffer();
        String s = str.append(h.getCommunityName()).append(COMMA).append(h.getBuildName()).append(COMMA).append(h.getUnitName()).append(COMMA).append(h.getFloorName()).append(COMMA).append(h.getName()).toString();
        return s ;
    }

    public static int compareResult(String a, String b) {
        int ia = 0, ib = 0;
        int nza = 0, nzb = 0;
        char ca, cb;
        int result;

        while (true) {
            // only count the number of zeroes leading the last number compared
            nza = nzb = 0;

            ca = charAt(a, ia);
            cb = charAt(b, ib);

            // skip over leading zeros
            while (ca == '0') {
                if (ca == '0') {
                    nza++;
                }
                else {
                    // only count consecutive zeroes
                    nza = 0;
                }

                // if the next character isn't a digit, then we've had a run of only zeros
                // we still need to treat this as a 0 for comparison purposes
                if (!Character.isDigit(charAt(a, ia + 1)))
                    break;

                ca = charAt(a, ++ia);
            }

            while (cb == '0') {
                if (cb == '0') {
                    nzb++;
                }
                else {
                    // only count consecutive zeroes
                    nzb = 0;
                }

                // if the next character isn't a digit, then we've had a run of only zeros
                // we still need to treat this as a 0 for comparison purposes
                if (!Character.isDigit(charAt(b, ib + 1)))
                    break;

                cb = charAt(b, ++ib);
            }

            // process run of digits
            if (Character.isDigit(ca) && Character.isDigit(cb)) {
                if ((result = compareRight(a.substring(ia), b.substring(ib))) != 0) {
                    return result;
                }
            }

            if (ca == 0 && cb == 0) {
                // The strings compare the same.  Perhaps the caller
                // will want to call strcmp to break the tie.
                return nza - nzb;
            }

            if (ca < cb) {
                return -1;
            }
            else if (ca > cb) {
                return +1;
            }

            ++ia;
            ++ib;
        }
    }

    private static char charAt(String s, int i) {
        if (i >= s.length()) {
            return 0;
        }
        else {
            return s.charAt(i) ;
        }
    }


}